/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package control;

import java.io.IOException;
import java.util.Collection;
import java.util.TreeSet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import modele.Dep;
import modele.Med;
import modele.Pays;
import modele.Spe;

/**
 *
 * @author wkhim
 */
public class Control extends HttpServlet {

    private Pays gsb;

    public void init(ServletConfig config) throws ServletException {
        super.init(config);
        gsb = new Pays();
    }

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String page = null;
        String action = request.getParameter("action");
        if ("listeMedDep".equals(action)) {
            String choix = request.getParameter("choix");
            if (choix == null) {
                Collection<Dep> list = gsb.getLesDeps();
                request.setAttribute("listeDeps", list);
                page = "listeMedDep.jsp";

            } else {
                Collection<Med> listeMed = gsb.getLeDep(choix).getLesMeds();
                request.setAttribute("listeMed", listeMed);
                request.setAttribute("dep", choix);
                page = "ResultMedDep.jsp";
            }
        } else if ("listeMedName".equals(action)) {
            String nom = request.getParameter("nom");
            if (nom == null) {
                page = "listeMedName.jsp";
            } else {
                Collection<Med> listeMed = new TreeSet<Med>();
                for (Dep unDep : gsb.getLesDeps()) {
                    listeMed.addAll(unDep.getLesMed(nom));
                }
                request.setAttribute("search", nom);
                request.setAttribute("listeMed", listeMed);
                page = "ResultMedName.jsp";
            }
        } else if ("listeMedSpe".equals(action)) {
            String choix = request.getParameter("choix");
            if (choix == null) {
                Collection<Spe> list = gsb.getLesSpes();
                request.setAttribute("listeSpes", list);
                page = "listeMedSpe.jsp";
            } else {
                Collection<Med> listeSpe = gsb.getLeSpe(choix).getLesMeds();
                request.setAttribute("listeMed", listeSpe);
                request.setAttribute("spe", choix);
                page = "ResultMedSpe.jsp";
            }
        } else {
            page = "index.jsp";
        }
        request.getRequestDispatcher(page).forward(request, response);
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
